/**
 * Created by Administrator on 2017/12/21 0021.
 */
var errorUrl = /环球网|1905电影网|中国会计视野|编织人生网|IT之家|优姿美女/;
var ua = navigator.userAgent.toLowerCase();
if (/newsid=[0-9]+/.test(location.href)) {
    var fromnewswid = location.href.match(/newsid=[0-9]+/)[0].split("=")[1];
    $.cookie("newsid", fromnewswid);
    $.cookie("qid", "");
    $.cookie("eid", "")
} else {
    if (/qid=[0-9]+/.test(location.href)) {
        var fromQid = location.href.match(/qid=[0-9]+/)[0].split("=")[1];
        $.cookie("newsid", "");
        $.cookie("qid", fromQid);
        $.cookie("eid", "")
    } else {
        if (/eid=[0-9]+/.test(location.href)) {
            var fromEid = location.href.match(/eid=[0-9]+/)[0].split("=")[1];
            $.cookie("newsid", "");
            $.cookie("qid", "");
            $.cookie("eid", fromEid)
        }
    }
}
function showloadimg(e) {
    $(e).fadeIn()
}
function saveScroll() {
    $.cookie("shareNewsListScroll", $(window).scrollTop())
}
var returnurl = encodeURIComponent("http://www.pulu.com/weixin/pulunews_share.html");
var gouserid = "";
function getUser() {
    var reg = new RegExp("code=[a-zA-Z0-9]*");
    if (window.location.href.match(reg)) {
        var code = window.location.href.match(reg);
        $.ajax({
            type: "GET",
            url: "GetOpenId.ashx?" + code,
            async: false,
            success: function(data) {
                var dat = data;
                $.cookie("wxod", dat);
                $.cookie("wxopenid", dat);
                $.ajax({
                    type: "GET",
                    url: getwxopenidurl + dat,
                    success: function(d) {
                        if (!d.Data || !d.Data.userId) {
                            if (!$.cookie("MYID") || $.cookie("MYID").length < 10) {
                                $.cookie("wxopenid", dat);
                                $.cookie("wx", "over");
                                if ($.cookie("newsid")) {
                                    location.href = "pulunews_share.html?newsid=" + $.cookie("newsid") + "&fromuser=" + $.cookie("fromuser")
                                } else {
                                    if ($.cookie("qid")) {
                                        location.href = "pulunews_share.html?qid=" + $.cookie("qid") + "&fromuser=" + $.cookie("fromuser")
                                    } else {
                                        if ($.cookie("eid")) {
                                            location.href = "pulunews_share.html?eid=" + $.cookie("eid") + "&fromuser=" + $.cookie("fromuser")
                                        }
                                    }
                                }
                            }
                        } else {
                            if (d.Data && d.Data.userId.length > 0) {
                                $.cookie("wxopenid", d.Data.openId);
                                $.cookie("MYID", d.Data.userId);
                                $.cookie("MYAVA", d.Data.userAva);
                                $.cookie("MYNICK", d.Data.userNick);
                                $.cookie("validateLevel",d.Data.validateLevel);
                                if ($.cookie("newsid")) {
                                    location.href = "http://www.pulu.com/weixin/pulunews_share.html?newsid=" + $.cookie("newsid") + "&fromuser=" + $.cookie("fromuser")
                                } else {
                                    if ($.cookie("qid")) {
                                        location.href = "http://www.pulu.com/weixin/pulunews_share.html?qid=" + $.cookie("qid") + "&fromuser=" + $.cookie("fromuser")
                                    } else {
                                        if ($.cookie("eid")) {
                                            location.href = "http://www.pulu.com/weixin/pulunews_share.html?eid=" + $.cookie("eid") + "&fromuser=" + $.cookie("fromuser")
                                        }
                                    }
                                }
                            }
                        }
                    }
                })
            }
        })
    } else {
        if ($.cookie("fromuser") && $.cookie("wx") == "over") {
            var thisFromuser = location.href.match(/fromuser=[0-9a-z]+/)[0].split("=")[1];
            loadPage()
        } else {
            var fromuserid = location.href.match(/fromuser=[a-z0-9]+/)[0].split("=")[1];
            $.cookie("fromuser", fromuserid);
            location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx658c34793ecfd9f9&redirect_uri=" + returnurl + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"
        }
    }
}
if (ua.match(/MicroMessenger/i) == "micromessenger") {
    if ($.cookie("MYID")) {
        var fromuserid = location.href.match(/fromuser=[a-z0-9]+/)[0].split("=")[1];
        $.cookie("fromuser", fromuserid);
        loadPage()
    }
    if (!$.cookie("MYID")) {
        getUser()
    }
} else {
    var fromuserid = location.href.match(/fromuser=[a-z0-9]+/)[0].split("=")[1];
    if (/newsid=[0-9]+/.test(location.href)) {
        var fromId = location.href.match(/newsid=[0-9]+/)[0]
    } else {
        if (/qid=[0-9]+/.test(location.href)) {
            var fromId = location.href.match(/qid=[0-9]+/)[0]
        } else {
            if (/eid=[0-9]+/.test(location.href)) {
                var fromId = location.href.match(/eid=[0-9]+/)[0]
            }
        }
    }
    $.cookie("fromuser", fromuserid);
    if (!/https/.test(location.href)) {
        location.href = "https://www.pulu.com/weixin/pulunews_share.html?" + fromId + "&fromuser=" + fromuserid
    } else {
        if ($.cookie("MYID")) {
            var fromuserid = location.href.match(/fromuser=[a-z0-9]+/)[0].split("=")[1];
            $.cookie("fromuser", fromuserid);
            loadPage()
        } else {
            loadPage()
        }
    }
}
function addNum() {
    saveScroll();
    if (thistype == 1) {
        var pcUrl = true;
        if (thisSiteName) {
            if (errorUrl.test(thisSiteName)) {
                pcUrl = false
            }
        }
        $.ajax({
            type: "post",
            datatype: "json",
            url: addcounturl,
            data: {
                "newsId": thisnewsid
            },
            success: function(d) {
                if (!pcUrl) {
                    location.href = "http://www.pulu.com/weixin/newsdetail.html?newsid=" + thisnewsid + "#fromuser=" + $.cookie("MYID")
                } else {
                    location.href = "newsdetail.html?newsid=" + thisnewsid + "#fromuser=" + $.cookie("MYID")
                }
            }
        })
    } else {
        if (thistype == 2) {
            location.href = "questiondetail.html?qid=" + thisnewsid + "#fromuser=" + $.cookie("MYID")
        } else {
            location.href = "shareexppage.html?eid=" + thisnewsid + "#fromuser=" + $.cookie("MYID")
        }
    }
}
var canclick3 = true;
var thisnewsid = "";
var thistype = "";
var thisSiteName = "";
function hrefto(i) {
    thisnewsid = i.dataset.id;
    thistype = i.dataset.type;
    if (i.dataset.sitename) {
        thisSiteName = i.dataset.sitename
    }
    if (/newsid=[0-9]+/.test(location.href)) {
        var fromnewswid = location.href.match(/newsid=[0-9]+/)[0].split("=")[1];
        $.cookie("newsid", fromnewswid)
    } else {
        if (/qid=[0-9]+/.test(location.href)) {
            var fromnewswid = location.href.match(/qid=[0-9]+/)[0].split("=")[1];
            $.cookie("qid", fromnewswid)
        } else {
            if (/eid=[0-9]+/.test(location.href)) {
                var fromnewswid = location.href.match(/eid=[0-9]+/)[0].split("=")[1];
                $.cookie("eid", fromnewswid)
            }
        }
    }
    if (thisnewsid == fromnewswid) {
        var urlnserid = location.href.match(/fromuser=[0-9a-z]+/)[0].split("=")[1];
        if (thistype == 1) {
            var pcUrl = true;
            if (thisSiteName) {
                if (errorUrl.test(thisSiteName)) {
                    pcUrl = false
                }
            }
            if (!pcUrl) {
                location.href = "http://www.pulu.com/weixin/newsdetail.html?newsid=" + thisnewsid + "#fromuser=" + urlnserid
            } else {
                location.href = "newsdetail.html?newsid=" + thisnewsid + "#fromuser=" + urlnserid
            }
        } else {
            if (thistype == 2) {
                location.href = "questiondetail.html?qid=" + thisnewsid + "#fromuser=" + urlnserid
            } else {
                location.href = "shareexppage.html?eid=" + thisnewsid + "#fromuser=" + urlnserid
            }
        }
    } else {
        if ($.cookie("MYID")) {
            if ($.cookie("MYID") != $.cookie("fromuser") && canclick3) {
                canclick3 = false;
                $.ajax({
                    type: "get",
                    url: click3addFriend + "?userId=" + $.cookie("MYID") + "&quserId=" + $.cookie("fromuser"),
                    datatype: "json",
                    success: function(data) {
                        canclick3 = true;
                        if (data.Data) {
                            $("#delmynews").css("display", "block")
                        } else {
                            addNum()
                        }
                    }
                })
            } else {
                addNum()
            }
        } else {
            $("#agreeDiv").css("display", "block")
        }
    }
}
function peoplepage(i) {
    var e = arguments.callee.caller.arguments[0] || event;
    if (e && e.stopPropagation) {
        e.stopPropagation()
    } else {
        if (window.event) {
            window.event.cancelBubble = true
        }
    }
    var userId = "";
    if (i.dataset.user) {
        userId = i.dataset.user
    } else {
        if (gouserid) {
            userId = gouserid
        } else {
            if ($.cookie("fromuser")) {
                userId = $.cookie("fromuser")
            }
        }
    }
    if ($.cookie("MYID")) {
        saveScroll();
        location.href = "otherpeoplepage.html?otherid=" + userId
    } else {
        $("#agreeDiv").css("display", "block")
    }
}
function seeTypeList(num) {
    //var e = arguments.callee.caller.arguments[0] || event;
    //if (e && e.stopPropagation) {
    //    e.stopPropagation()
    //} else {
    //    if (window.event) {
    //        window.event.cancelBubble = true
    //    }
    //}
    //var userId = "";
    //if (gouserid) {
    //    userId = gouserid
    //} else {
    //    if ($.cookie("fromuser")) {
    //        userId = $.cookie("fromuser")
    //    }
    //}
    //if ($.cookie("MYID")) {
    //    saveScroll();
    //    location.href = "peopleTypeList.html?otherid=" + userId + "&qtype=" + num
    //} else {
    //    $("#agreeDiv").css("display", "block")
    //}
}
$("#agreeno").click(function() {
    $("#agreeDiv").css("display", "none")
});
$("#agreeok").click(function() {
    $("#agreeDiv").css("display", "none");
    location.href = "http://www.pulu.com/weixin/index.html#fromuser=" + $.cookie("fromuser")
});
$("#delmynewsok").click(function() {
    var mynick = "";
    if ($.cookie("MYNICK")) {
        mynick = $.cookie("MYNICK")
    }
    conn.subscribe({
        to: $.cookie("fromuser"),
        message: mynick + "已经阅读你的pulu3次，请求加为好友"
    });
    $.ajax({
        type: "post",
        url: savefriendnoteurl0,
        data: {
            "userId": $.cookie("MYID"),
            "friendId": $.cookie("fromuser"),
            "msg": ""
        },
        success: function(d) {
            $.ajax({
                type: "post",
                url: closeClick3,
                datatype: "json",
                data: {
                    "userId": $.cookie("MYID"),
                    "quserId": $.cookie("fromuser")
                },
                success: function(data) {
                    $("#delmynews").css("display", "none");
                    addNum()
                }
            })
        }
    })
});
$("#delmynewscancel").click(function() {
    $.ajax({
        type: "post",
        url: closeClick3,
        datatype: "json",
        data: {
            "userId": $.cookie("MYID"),
            "quserId": $.cookie("fromuser")
        },
        success: function(data) {
            $("#delmynews").css("display", "none");
            addNum()
        }
    })
});
function loadPage() {
    var thisUserId = location.href.match(/fromuser=[0-9a-z]+/)[0].split("=")[1];
    var friendId = thisUserId;
    if ($.cookie("fromuser") && !/undefined/.test($.cookie("fromuser"))) {
        if ($.cookie("fromuser") != friendId) {
            $.cookie("fromuser", friendId)
        }
        friendId = $.cookie("fromuser")
    }
    if ($.cookie("MYNICK")) {
        $("#delmynews span#threeFriendMy").html($.cookie("MYNICK"))
    }
    $.ajax({
        type: "get",
        url: "https://www.pulu.com/webapi/api/puluuser/GetUserInfo?userId=" + thisUserId + "&friendId=" + friendId,
        datatype: "json",
        success: function(data) {
            var fromName = data.Data.userNick;
            var realName = "";
            if (data.Data.realName) {
                realName = "（" + data.Data.realName + "）"
            }
            $("#fromNick").html(fromName + realName);
            $("#threeFriendNick").html(fromName + realName);
            var loadId = "";
            var getType = "";
            if ($.cookie("newsid")) {
                loadId = $.cookie("newsid");
                getType = 1
            } else {
                if ($.cookie("qid")) {
                    loadId = $.cookie("qid");
                    getType = 2
                } else {
                    if ($.cookie("eid")) {
                        loadId = $.cookie("eid");
                        getType = 3
                    }
                }
            }
            $.ajax({
                type: "get",
                datatype: "json",
                url: "https://www.pulu.com/webapi/api/puluuser/GetPulu_10log_dealByUserId?newsid=" + loadId + "&userId=" + friendId + "&qtype=" + getType,
                success: function(data) {
                    if (data.Data == "" || !data.Data) {
                        $("#waiting").css("display", "none");
                        $("#nonews").css("display", "block")
                    } else {
                        $("title").html("PULU—" + data.Data[0].title);
                        var allhtmlword = "";
                        for (var i = 0; i < data.Data.length; i++) {
                            var tim = "";
                            if (data.Data[i].cTime) {
                                tim = getDateDiff(getDateTimeStamp(data.Data[i].cTime))
                            }
                            if (tim) {
                                tim = "<span class='rt'>" + tim + "</span>"
                            } else {
                                tim = "<span class='rt'>刚刚</span>"
                            }
                            var activeType = "转发";
                            if (data.Data[i].title) {
                                if (data.Data[i].operateTypeName) {
                                    activeType = data.Data[i].operateTypeName
                                }
                                if (data.Data[i].type == 1) {
                                    if (data.Data[i].imgurl) {
                                        allhtmlword += "<div class='newsbox'><div class='tpl-2' onclick='hrefto(this)' data-id='" + data.Data[i].qid + "' data-sitename='" + data.Data[i].sitename + "' data-type='1'><a><div class='clr'><div class='info-content rt'><div class='text-content' ><h2><p>" + data.Data[i].title + "</p></h2></div><div class='news-from'><span class='src-net' style='margin-right: 5px;'><i style='color: #41A5ED;' onclick='peoplepage(this)'>" + fromName +  "</i>" + activeType + "<span class='seeTypeList' onclick='seeTypeList(1)'>资讯</span></span>" + tim + "</div></div><div class='pic-content'><img src='" + data.Data[i].imgurl + "'/></div></div></a></div></div>"
                                    } else {
                                        allhtmlword += "<div class='newsbox'><div class='tpl-3' data-id='" + data.Data[i].qid + "' onclick='hrefto(this)' data-sitename='" + data.Data[i].sitename + "' data-type='1'><a><div class='text-content'><h2><p>" + data.Data[i].title + "</p></h2></div><div class='news-from'><span class='src-net' style='margin-right: 5px;'><i style='color: #41A5ED;' onclick='peoplepage(this)'>" + fromName + "</i>" + activeType + "<span class='seeTypeList' onclick='seeTypeList(1)'>资讯</span></span>" + tim + "</div></a></div></div>"
                                    }
                                } else {
                                    var typeName = "";
                                    var typeNum = data.Data[i].type;
                                    if (data.Data[i].type == 1) {
                                        typeName = "<span class='seeTypeList' onclick='seeTypeList(1)'>新闻</span>"
                                    } else {
                                        if (data.Data[i].type == 2) {
                                            typeName = "<span class='seeTypeList' onclick='seeTypeList(2)'>悬赏</span>"
                                        } else {
                                            if (data.Data[i].type == 3) {
                                                typeName = "<span class='seeTypeList' onclick='seeTypeList(3)'>经验</span>"
                                            } else {
                                                typeName = "<span class='seeTypeList' onclick='seeTypeList(4)'>资源</span>"
                                            }
                                        }
                                    }
                                    var tit = codetoface(data.Data[i].title);
                                    var datauser = "";
                                    if (data.Data[i].currentUserId) {
                                        datauser = data.Data[i].currentUserId;
                                        gouserid = data.Data[i].currentUserId
                                    }
                                    var keywordSpan = "";
                                    if (data.Data[i].keywords) {
                                        var changeKeyWord = data.Data[i].keywords.split(",").join("|");
                                        keywordSpan = "<span class='keyword' data-eid='" + data.Data[i].qid + "' data-qtype='" + data.Data[i].type + "' onclick='gotoKeyWordList(this)'>" + changeKeyWord + "| </span>"
                                    }
                                    allhtmlword += "<div class='newsbox'><div class='tpl-3' data-id='" + data.Data[i].qid + "' onclick='hrefto(this)' data-type='" + typeNum + "'><a><div class='text-content'><h2>" + "<p>" + keywordSpan + tit + "</p></h2></div><div class='news-from'><span class='src-net' style='margin-right: 5px;'><i style='color: #41A5ED;' onclick='peoplepage(this)' data-user='" + datauser + "'>" + fromName +  "</i>" + activeType + typeName + "</span>" + tim + "</div></a></div></div>"
                                }
                            }
                        }
                        $("#pleasewait").before(allhtmlword);
                        if ($.cookie("shareNewsListScroll")) {
                            $(window).scrollTop($.cookie("shareNewsListScroll"));
                            $.cookie("shareNewsListScroll", "")
                        }
                    }
                    $("#pleasewait").css("display", "none");
                    if (/[0-9]/.test($.cookie("shareNewsListScroll"))) {
                        $(window).scrollTop($.cookie("shareNewsListScroll"));
                        $.cookie("shareNewsListScroll", "")
                    }
                }
            })
        }
    })
}
$("#logo1").click(function() {
    if ($.cookie("MYID")) {
        location.href = "pulu.html"
    } else {
        location.href = "http://www.pulu.com/weixin/login.html#fromuser=" + $.cookie("fromuser")
    }
});
function gotoKeyWordList(i) {
    var e = arguments.callee.caller.arguments[0] || event;
    if (e && e.stopPropagation) {
        e.stopPropagation()
    } else {
        if (window.event) {
            window.event.cancelBubble = true
        }
    }
    if ($.cookie("MYID")) {
        $.cookie("keywordtype", "");
        $.cookie("keywordid", "");
        $.cookie("kyscrollTop", "");
        $.cookie("kycurrentIndex", "");
        location.href = "keywordslist.html?type=" + i.dataset.qtype + "&eid=" + i.dataset.eid
    } else {
        location.href = "http://www.pulu.com/weixin/login.html#fromuser=" + $.cookie("fromuser")
    }
}
;